package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.jobs;
import com.example.springboot.service.JobSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 职位搜索Controller
 *
 * @author 柏旭东
 * @date 2024-6-20
 */

@CrossOrigin(origins = "http://localhost:7070")
@RequestMapping("/JobSearch")
@RestController
public class JobSearchController {
    private final JobSearchService jobSearchService;

    @Autowired
    public JobSearchController(JobSearchService jobSearchService) {
        this.jobSearchService = jobSearchService;
    }

    /**
     *
     * 所有岗位信息
     */
    @GetMapping("/findAllJobs")
    public Result findAllJobs() {
        try {
            List<jobs> jobsList = jobSearchService.findAllJobs();
            return Result.success(jobsList);
        } catch (Exception e) {
            return Result.error("系统异常，请稍后再试");
        }
    }



    // 根据薪资排序搜索岗位
    @GetMapping("/search/salary")
    public Result findJobsBySalary(@RequestParam String order) {
        try {
            List<jobs> jobsList = jobSearchService.findJobsBySalary(order);
            return jobsList != null ? Result.success(jobsList) : Result.error("没有找到职位");
        } catch (Exception e) {
            return Result.error("系统异常，请稍后再试");
        }
    }

    // 根据发布日期排序搜索岗位
    @GetMapping("/search/publish-date")
    public Result findJobsByPublishDate(@RequestParam String order) {
        try {
            List<jobs> jobsList = jobSearchService.findJobsByPublishDate(order);
            return jobsList != null ? Result.success(jobsList) : Result.error("没有找到职位");
        } catch (Exception e) {
            return Result.error("系统异常，请稍后再试");
        }
    }

    // 根据工作地点搜索职位
    @GetMapping("/search/location")
    public Result findJobsByLocation(@RequestParam String location) {
        try {
            List<jobs> jobsList = jobSearchService.findJobsByLocation(location);
            return jobsList != null ? Result.success(jobsList) : Result.error("没有找到职位");
        } catch (Exception e) {
            return Result.error("系统异常，请稍后再试");
        }
    }

    // 根据公司id搜索职位
    @GetMapping("/company")
    public Result findJobsByCompanyId(@RequestParam Long companyId) {
        try {
            List<jobs> jobsList = jobSearchService.findJobsByCompanyId(companyId);
            return jobsList != null ? Result.success(jobsList) : Result.error("没有找到职位");
        } catch (Exception e) {
            return Result.error("系统异常，请稍后再试");
        }
    }

    // 根据职位id搜索
    @GetMapping("/search/job-id/{jobId}")
    public Result findJobById(@PathVariable Long jobId) {
        try {
            jobs job = jobSearchService.findJobById(jobId);
            if (job == null) {
                // 如果职位不存在，返回错误信息
                return Result.error("职位未找到");
            }
            // 如果职位存在，返回成功信息和职位数据
            return Result.success(job);
        } catch (Exception e) {
            // 如果出现异常，返回系统错误信息
            return Result.error("系统异常，请稍后再试");
        }
    }



    // 根据职位标题搜索
    @GetMapping("/search/job-title")
    public Result findJobsByJobTitle(@RequestParam String jobTitle) {
        try {
            List<jobs> jobsList = jobSearchService.findJobsByJobTitle(jobTitle);
            if (jobsList == null || jobsList.isEmpty()) {
                // 如果没有找到职位，返回错误信息
                return Result.error("没有找到职位");
            }
            // 如果找到了职位，返回成功信息和职位列表数据
            return Result.success(jobsList);
        } catch (Exception e) {
            // 如果出现异常，返回系统错误信息
            return Result.error("系统异常，请稍后再试");
        }
    }
}
